from flask import Flask, request
from flask_restful import Api, Resource

# 参考博客 http://www.bjhee.com/flask-ext1.html
app = Flask(__name__)
#
api = Api(app)

USER_LIST = {
    '1': {'name': 'Michael'},
    '2': {'name': 'Tom'},
}


class UserList(Resource):
    def get(self):
        return USER_LIST

    def post(self):
        user_id = int(max(USER_LIST.keys())) + 1
        user_id = '%i' % user_id
        USER_LIST[user_id] = {'name': request.form['name']}
        return USER_LIST[user_id]


# 1.Restful扩展通过api.add_resource()方法添加路由。(类名【继承Resource类名】，URL路径)
api.add_resource(UserList, '/users')

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)
